home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Tools / tables / abbrev / abbrev.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.8 KB  |  227 lines

  1. /* abbrev.c: abbreviates domains */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Tools/tables/abbrev/RCS/abbrev.c,v 6.0 1991/12/18 20:33:28 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Tools/tables/abbrev/RCS/abbrev.c,v 6.0 1991/12/18 20:33:28 jpo Rel $
  9.  *
  10.  * $Log: abbrev.c,v $
  11.  * Revision 6.0  1991/12/18  20:33:28  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include    "util.h"
  19.  
  20. extern char    *strdup (), *index();
  21. static int    LineCount;
  22. static char    *myname;
  23. static char    *aliases[100];
  24. static int    maxalias = 0;
  25. static int    bitmap;
  26.  
  27. static void do_alias ();
  28. /* ---------------------  Begin     Routines  -------------------------------- */
  29.  
  30.  
  31. main (ac,av)
  32. int    ac;
  33. char    *av [];
  34. {
  35.     char    buffer [BUFSIZ], *entries[10];
  36.     register char    *alias, *name, *nonwildcard;
  37.     register int    i, NumEntries, use;
  38.     char    rhs[BUFSIZ], entry[BUFSIZ];
  39.  
  40.     myname = av [0];
  41.     if (ac < 2) {
  42.         fprintf (stderr, "Usage: %s Domain...\n", myname);
  43.         exit (1);
  44.     }
  45.  
  46.  
  47.     for (i = 1; i < ac; i++) {
  48.         (void) sprintf (buffer, "X.%s", av[i]);
  49.         aliases[maxalias++] = strdup (buffer);
  50.     }
  51.     LineCount = 0;
  52.  
  53.     while (fgets (buffer, sizeof buffer, stdin) != NULL) {
  54.         LineCount++;
  55.         alias = &buffer [0];
  56.         printf ("%s", alias);
  57.         if (*alias == '#' || *alias == '\n')
  58.             continue;
  59.  
  60.         alias [strlen (alias) - 1] = '\0';
  61.         for (name = alias; *name != '\0'; name++)
  62.             if (*name == ':' || *name == '#')
  63.                 break;
  64.         if (*name == '#')
  65.             continue;
  66.  
  67.         if (*name == '\0') {
  68.             fprintf (stderr, "%s: Syntax Error [%s] ",
  69.                 myname, alias);
  70.             fprintf (stderr, "at line %d - Ignored\n", LineCount);
  71.             continue;
  72.         }
  73.         *name++ = '\0';
  74.  
  75.         if (alias[0] == '*'
  76.             && alias[1] == '.')
  77.             nonwildcard = &(alias[2]);
  78.         else
  79.             nonwildcard = &(alias[0]);
  80.  
  81.         if ((NumEntries = sstr2arg (name, 10, entries, "|")) < 1) {
  82.             fprintf(stderr, "%s: Syntax Error [%s] ",
  83.                 myname, alias);
  84.             fprintf(stderr, "at line %d - Ignored\n", LineCount);
  85.             continue;
  86.         }
  87.         rhs[0] = '\0';
  88.         use = 1;
  89.         for (i = 0; i < NumEntries && use == 1; i++) {
  90.             use = do_entry(entries[i], nonwildcard, entry, alias);
  91.             if (rhs[0] != '\0')
  92.                 strcat (rhs, "|");
  93.             strcat (rhs, entry);
  94.         }
  95.         if (use == 1)
  96.             for (i = 0; i < maxalias; i++)
  97.                 do_alias (alias, rhs, aliases[i]);
  98.     }
  99.     exit (0);
  100. }
  101.  
  102. do_entry(name, nonwildcard, rhs, alias)
  103. char    *name;
  104. char     *nonwildcard;
  105. char    *rhs;
  106. char    *alias;
  107. {
  108.     char    *vec[10];
  109.     int    vecp, i;
  110.     int    use = 0;
  111.     int    gotnorm;
  112.     int    mtaislhs;
  113.     if ((vecp = sstr2arg (name, 10, vec, " \t")) < 1) {
  114.         fprintf(stderr, "%s: Syntax Error [%s] ",
  115.             myname, alias);
  116.         fprintf(stderr, "at line %d - Ignored\n", LineCount);
  117.         return use;
  118.     }
  119.         
  120.     rhs[0] = '\0';
  121.     gotnorm = 0;
  122.     mtaislhs = 0;
  123.     for (i = 0; i < vecp; i++) {
  124.         char *p;
  125.  
  126.         if (lexnequ (vec[i], "norm+mta", 8) == 0
  127.               || lexnequ (vec[i], "mta+norm", 8) == 0) { 
  128.             use = 1;
  129.             gotnorm = 1;
  130.             if (rhs[0] != '\0')
  131.                 strcat(rhs, " ");
  132.             if ((p = index(vec[i], '=')) != (char *) 0
  133.                 && *(p+1) != NULL)
  134.                 strcat(rhs, vec[i]);
  135.             else {
  136.                 /* construct norm+mta= */
  137.                 strcat(rhs, "norm+mta=");
  138.                 strcat(rhs, nonwildcard);
  139.             }
  140.         } else if (lexnequ (vec[i], "norm", 3) == 0) {
  141.             use = 1;
  142.             gotnorm = 1;
  143.             if (rhs[0] != '\0')
  144.                 strcat(rhs, " ");
  145.             if ((p = index(vec[i], '=')) != (char *) 0
  146.                 && *(p+1) != NULL)
  147.                 strcat(rhs, vec[i]);
  148.             else {
  149.                 /* construct norm= */
  150.                 strcat(rhs, "norm=");
  151.                 strcat(rhs, nonwildcard);
  152.             }
  153.         } else if (lexnequ (vec[i], "synonym", 7) == 0) {
  154.             gotnorm = 1;
  155.             use = 1;
  156.             if (rhs[0] != '\0')
  157.                 strcat(rhs, " ");
  158.             strcat(rhs, vec[i]);
  159.         } else if (lexnequ (vec[i], "local", 5) == 0) {
  160.             use = 1;
  161.             if (rhs[0] != '\0')
  162.                 strcat(rhs, " ");
  163.             strcat(rhs, vec[i]);
  164.         } else if (lexnequ (vec[i], "mta", 3) == 0) {
  165.             use = 1;
  166.             if (rhs[0] != '\0')
  167.                 strcat(rhs, " ");
  168.             if ((p = index(vec[i], '=')) != (char *) 0
  169.                 && *(p+1) != NULL)
  170.                 strcat(rhs, vec[i]);
  171.             else 
  172.                 /* construct norm= */
  173.                 mtaislhs = 1;
  174.         } else {
  175.             if (rhs[0] != '\0')
  176.                 strcat(rhs, " ");
  177.             strcat(rhs, vec[i]);
  178.         }
  179.     }
  180.     bitmap = 0;
  181.  
  182.     if (gotnorm == 0) {
  183.         char    *key;
  184.         /* add norm=lhs */
  185.         if (mtaislhs == 1) 
  186.             key = "norm+mta=";
  187.         else
  188.             key = "norm=";
  189.         if (rhs[0] != '\0')
  190.             strcat(rhs, " ");
  191.         strcat(rhs, key);
  192.         strcat(rhs, nonwildcard);
  193.     } else if (mtaislhs == 1) {
  194.         if (rhs[0] != '\0')
  195.             strcat(rhs, " mta=");
  196.         else
  197.             strcat(rhs, "mta=");
  198.         strcat(rhs, nonwildcard);
  199.     }
  200.     return use;
  201. }
  202.  
  203. /* ---------------------  Static  Routines  ------------------------------- */
  204.  
  205.  
  206.  
  207. static void do_alias (alias, name, domain)
  208. register char    *alias, *name, *domain;
  209. {
  210.     register char    *a, *d;
  211.     int dn = -1;
  212.  
  213.     a = &alias [strlen (alias) - 1];
  214.     d = &domain [strlen (domain) - 1];
  215.     for (; a != alias && *a == *d; a--, d--)
  216.         if (*a == '.') {
  217.             dn ++;
  218.             if (bitmap & (1 << dn))
  219.                 continue;
  220.             bitmap |= (1 << dn);
  221.             *a = '\00';
  222.             if (strcmp (alias, "*") != 0)
  223.                 printf ("%s:%s\n", alias, name);
  224.             *a = '.';
  225.         }
  226. }
  227.